﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyThuVien.Supporter;
using System.Data.SqlClient;
using System.Data;
using QuanLyThuVien.DTO;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng NGUOIDUNG
    //----------------------------------------------------------------------
    public class DALNguoiDung
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALNguoiDung()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành NGUOIDUNG
        //----------------------------------------------------------------------
        public DTONguoiDung ConvertToNguoiDung(DataRow row)
        {
            DTONguoiDung nguoidung = new DTONguoiDung();
            nguoidung.TaiKhoan = row["TaiKhoan"].ToString();
            nguoidung.MatKhau = row["MatKhau"].ToString();
            nguoidung.MaNhom = row["MaNhom"].ToString();
            nguoidung.MaDG = row["MaDocGia"].ToString();
            if (nguoidung.MaNhom == "NH01")
                nguoidung.LoaiNguoiDung = LOAI_NGUOI_DUNG.NguoiQuanTri;
            else if (nguoidung.MaNhom == "NH02")
                nguoidung.LoaiNguoiDung = LOAI_NGUOI_DUNG.ThuThu;
            else if (nguoidung.MaNhom == "NH03")
                nguoidung.LoaiNguoiDung = LOAI_NGUOI_DUNG.DocGia;
            else
                nguoidung.LoaiNguoiDung = LOAI_NGUOI_DUNG.DocGia;
            return nguoidung;
        }

        //----------------------------------------------------------------------
        //Desc: Đếm bộ tài khoản - mật khẩu
        //----------------------------------------------------------------------
        public int DemTK_MK(string taikhoan, string matkhau)
        {
            if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spNguoiDung_DemTaiKhoan_MatKhau";
            SqlParameter sqlprTaiKhoan = new SqlParameter("@TaiKhoan", SqlDbType.VarChar, 16) { Value = taikhoan };
            SqlParameter sqlprMatKhau = new SqlParameter("@MatKhau", SqlDbType.VarChar, 36) { Value = matkhau };
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName, sqlprTaiKhoan, sqlprMatKhau);

            if (obj == null)
                return -1;
            int dem;
            try
            {
                dem = (int)obj;
            }
            catch
            {
                return -1;
            }
            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: lấy thông tin người dùng
        //----------------------------------------------------------------------
        public DTONguoiDung LayThongTinNguoiDung(string taikhoan, string matkhau)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spNguoiDung_LayThongTinNguoiDung";
            SqlParameter sqlprTaiKhoan = new SqlParameter("@TaiKhoan", SqlDbType.VarChar, 16) { Value = taikhoan };
            SqlParameter sqlprMatKhau = new SqlParameter("@MatKhau", SqlDbType.VarChar, 36) { Value = matkhau };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprTaiKhoan, sqlprMatKhau);
            if (data == null || data.Rows.Count == 0)
                return null;
            return ConvertToNguoiDung(data.Rows[0]);
        }

        //----------------------------------------------------------------------
        //Desc: đếm mã tài khoản
        //----------------------------------------------------------------------
        public int DemMaTaiKhoan(string matk)
        {
            if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spNguoiDung_DemMaTaiKhoan";
            SqlParameter sqlprTaiKhoan = new SqlParameter("@TaiKhoan", SqlDbType.VarChar, 16) { Value = matk };
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName, sqlprTaiKhoan);
            if (obj == null)
                return -1;
            int dem;
            try
            {
                dem = (int)obj;
            }
            catch { return -1; }
            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật người dùng
        //----------------------------------------------------------------------
        public bool CapNhat(DTONguoiDung nguoidung)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spNguoiDung_CapNhatNguoiDung";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int){ Value = 1 };
            SqlParameter sqlprTaiKhoan = new SqlParameter("@TaiKhoan", SqlDbType.VarChar, 16)
            { Value = nguoidung.TaiKhoan};
            SqlParameter sqlprMatKhau = new SqlParameter("@MatKhau", SqlDbType.VarChar, 36)
            { Value = nguoidung.MatKhau };
            SqlParameter sqlprMaNhom = new SqlParameter("@MaNhom", SqlDbType.VarChar, 15)
            { Value = nguoidung.MaNhom};
            SqlParameter sqlprMaDocGia = new SqlParameter("@MaDocGia", SqlDbType.VarChar, 15);
            if (nguoidung.LoaiNguoiDung == LOAI_NGUOI_DUNG.DocGia)
                sqlprMaDocGia.Value = nguoidung.MaDG;
            else
                sqlprMaDocGia.Value = null;

            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprTaiKhoan, sqlprMatKhau,
                sqlprMaNhom, sqlprMaDocGia);
        }

        //----------------------------------------------------------------------
        //Desc: xóa người dùng
        //----------------------------------------------------------------------
        public bool Xoa(DTONguoiDung nguoidung)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spNguoiDung_CapNhatNguoiDung";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 0 };
            SqlParameter sqlprTaiKhoan = new SqlParameter("@TaiKhoan", SqlDbType.VarChar, 16) { Value = nguoidung.TaiKhoan };
            SqlParameter sqlprMatKhau = new SqlParameter("@MatKhau", SqlDbType.VarChar, 36) { Value = nguoidung.MatKhau };
            SqlParameter sqlprMaNhom = new SqlParameter("@MaNhom", SqlDbType.VarChar, 15) { Value = nguoidung.MaNhom };
            SqlParameter sqlprMaDocGia = new SqlParameter("@MaDocGia", SqlDbType.VarChar, 15) { Value = nguoidung.MaDG };

            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprTaiKhoan, sqlprMatKhau,
                sqlprMaNhom, sqlprMaDocGia);
        }
    }
}
